package com.abing.starry.strategy.api;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.abing.api.client.StarryApiClient;
import com.abing.api.model.StarryApiRequest;
import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author CaptainBing
 * @Date 2024/8/24 22:10
 * @Description
 */
@Slf4j
@RequiredArgsConstructor
@InvokeStrategyConfig(strategy = InvokeStrategyEnum.DEFAULT)
public class DefaultInvokeStrategy implements InvokeStrategy {

    private final StarryApiClient starryApiClient;

    @Override
    public String doInvoke(StarryApiRequest starryApiRequest) {

        HttpRequest httpRequest = starryApiClient.onAssemblyRequest(starryApiRequest);

        HttpResponse httpResponse = httpRequest.execute();
        boolean responseOk = httpResponse.isOk();
        ThrowUtils.throwIf(!responseOk,ErrorCode.OPERATION_ERROR, "接口调用失败");
        return httpResponse.body();
    }
}
